package com.zxx.xin.shorthand.ui.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.zxx.xin.shorthand.R;
import com.zxx.xin.shorthand.database.bean.Daily;
import com.zxx.xin.shorthand.database.DatabaseHelper;
import com.zxx.xin.shorthand.helper.BitmapHleper;
import com.zxx.xin.shorthand.helper.FileUtils;

/**
 * Created by xin on 2018/3/19.
 */

public class DailyWriteActivity extends AppCompatActivity implements View.OnClickListener {

    private int mRequestCode = 1001;

    private EditText mTitleView;

    private EditText mContentView;

    private Daily mDaily;

    private LinearLayout mBgLayout;

    private String mBgBitmap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily_write);

        mDaily = getIntent().getParcelableExtra("data");

        Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/jianshi_default.otf");

        mTitleView = (EditText) findViewById(R.id.title);
        mTitleView.setTypeface(typeface);

        mContentView = (EditText) findViewById(R.id.content);
        mContentView.setTypeface(typeface);

        mBgLayout = (LinearLayout) findViewById(R.id.bg);

        findViewById(R.id.save).setOnClickListener(this);
        findViewById(R.id.pic).setOnClickListener(this);

        if (mDaily != null) {
            mTitleView.setText(mDaily.getMTitle());
            mContentView.setText(mDaily.getMContent());
            mBgBitmap = mDaily.getmBgBitmap();
            mBgLayout.setBackground(new BitmapDrawable(BitmapHleper.getBitmapByFilePath(this, mDaily.getMBgBitmap(), R.drawable.write_bg)));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save:
                if (TextUtils.isEmpty(mTitleView.getText())) {
                    Toast.makeText(this, getString(R.string.tip), Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mDaily == null) {
                    Daily daily = new Daily(null, mTitleView.getText().toString(), mContentView.getText().toString(), System.currentTimeMillis(), mBgBitmap);
                    DatabaseHelper.getInstance().insertDaily(daily);
                } else {
                    mDaily.setMTitle(mTitleView.getText().toString());
                    mDaily.setMContent(mContentView.getText().toString());
                    mDaily.setmBgBitmap(mBgBitmap);
                    DatabaseHelper.getInstance().updateDaily(mDaily);
                }
                finish();
                break;
            case R.id.pic:
                chooseBackground();
                break;
        }
    }

    private void chooseBackground() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, mRequestCode);
    }

    @Override
    public void onBackPressed() {
        AlertDialog dialog = new AlertDialog.Builder(this).setTitle(R.string.save_tip).setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                findViewById(R.id.save).performClick();
            }
        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == mRequestCode) {
            if (data != null){
                Uri uri = data.getData();
                mBgBitmap = FileUtils.getFilePathByUri(this, uri);
                mBgLayout.setBackground(new BitmapDrawable(BitmapHleper.getBitmapByFilePath(this, mBgBitmap, R.drawable.write_bg)));
            }
        }
    }

    public static void startPage(Context context, Daily daily) {
        Intent intent = new Intent(context, DailyWriteActivity.class);
        if (daily != null) {
            intent.putExtra("data", daily);
        }
        context.startActivity(intent);
    }
}
